const mixin = require("../../mixins");
Component({
  behaviors: [mixin],
  data: {
    currIndex: -1,
    scrollLeft: 0,
    scrollTop: 0,
    isIDE: !1,
    duration: 0,
    scrollDistance: 0,
    transX: 0
  },
  properties: {
    mf_data: Object,
    utoolsData: Object
  },
  attached() {
	  let {
	    duration: t = 300,children
	  } = this.properties.mf_data;

	  let tabbarbg={}

		for(let i = 0; i < children.length; i++) {
			tabbarbg =  children[i].children.find(item=>item.name1=='tabbarbg')
			if(tabbarbg) {
				break
			}
		}
	  let systemInfo = wx.getSystemInfoSync();
	  let statusBarHeight = systemInfo.statusBarHeight;
	  let rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null; //胶囊按钮位置信息
	  wx.getMenuButtonBoundingClientRect();
	  let navBarHeight = (function() {
	    //导航栏高度
	    let gap = rect.top - systemInfo.statusBarHeight; //动态计算每台手机状态栏到胶囊按钮间距
	    return 2 * gap + rect.height;
	  })();
	  let titleBarHeight = navBarHeight;
    // 探索页 tab 跳转



    const DomPage = getCurrentPages()
    const navName = DomPage[DomPage.length - 1].options.navName
    // if (navName && this.properties.utoolsData[0].title) {
      const titleData = this.properties.utoolsData[0].title.children
      let selectedIdx = titleData.findIndex(i => i.name1 == navName)
	  selectedIdx = selectedIdx <0?0 :selectedIdx

      this.setData({
        currIndex: Number(selectedIdx)||0,
		top: statusBarHeight+ titleBarHeight,
		tabbarbg,
		isIDE: "devtools" == wx.getSystemInfoSync().platform,
		duration: t
      })
    // }

	this.autoPlay()
  },
  didUpdate(t, e) {},
  detached() {},
  methods: {
	  init(){


	  },
    autoPlay() {
      const {
        autoPlay: t = !1,
        autoPlayInterval: e = 3e3
      } = this.properties.mf_data;
      t && (this.timer = setInterval(() => {
        this.handleTouchEnd(-1)
      }, e))
    },
    bindClick(t) {
      const {
        index: e
      } = t.currentTarget.dataset, {
        duration: a = 300,
        title: {
          children
        }
      } = this.properties.mf_data;

      this.switch || (this.timerTouch(a), this.setData({
        scrollLeft: this.translateDistance(Number(e)),
        currIndex: Number(e)
      }))
      if (children[e]?.trackingParams) {
        this.triggerEvent('triggerComponent', {
          data: {
            trackData: children[e]?.trackingParams,
          }
        }, {
          bubbles: true,
          composed: true
        });
      }
      if(wx.pageScrollTo){
        wx.pageScrollTo({
          scrollTop: 0
        })
      }
    },
    translateDistance(t = 0) {
      let e, a = this.properties.mf_data.title,
        i = Number(a.children[0].style.width),
        s = i * a.children.length;
      e = i * (t + 1) >= 726 && i * (t + 1) <= s ? t <= a.children.length - 2 ? t - 1 : t - 2 : 0;
      let n = i * e > 0 ? i * e : 0;
      return Number(n)
    },
    touchStart(t) {
      const {
        clientX: e
      } = t.changedTouches[0];
      this.startX = e
    },
    touchMove() {},
    touchEnd(t) {
      if (Date.now() - this.startTime < this.data.duration) return;
      const {
        clientX: e
      } = t.changedTouches[0], a = e - this.startX;
      Math.abs(a) < 20 || (this.startTime = Date.now(), this.handleTouchEnd(a))
    },
    timerTouch(t = 200) {
      this.switch = !0, setTimeout(() => {
        this.switch = !1
      }, t)
    },
    handleTouchEnd(t) {
      const {
        animationType: e
      } = this.properties.mf_data;
      switch (e) {
        case "渐隐渐显":
          this.handleAnimationTypeByOpacity(t);
          break;
        case "滑动滚动":
          this.handleAnimationTypeByTranslate(t)
      }
    },
    handleAnimationTypeByOpacity(t) {
      let {
        currIndex: e
      } = this.data;
      const {
        children: a = []
      } = this.properties.mf_data;
      let i = Number(e);
      t > 0 ? 0 === i ? i = a.length - 1 : i -= 1 : i === a.length - 1 ? i = 0 : i += 1;
      const s = {
        currentTarget: {
          dataset: {
            index: i
          }
        }
      };
      this.bindClick(s)
    },
    handleAnimationTypeByTranslate(t) {
      let {
        currIndex: e
      } = this.data;
      const {
        children: a = []
      } = this.properties.mf_data;
      let i = Number(e);
      if (t > 0) {
        if (0 === i) return void this.setData({
          currIndex: i - 1,
          scrollLeft: this.translateDistance(a.length - 1)
        }, () => {
          setTimeout(() => {
            this.setData({
              currIndex: a.length - 1,
              duration: 0
            }, () => {
              setTimeout(() => {
                this.setData({
                  duration: this.properties
                    .mf_data.duration || 300
                })
              }, 16.7)
            })
          }, this.data.duration)
        });
        i -= 1
      } else {
        if (i === a.length - 1) return void this.setData({
          currIndex: i + 1,
          scrollLeft: this.translateDistance(0)
        }, () => {
          setTimeout(() => {
            this.setData({
              currIndex: 0,
              duration: 0
            }, () => {
              setTimeout(() => {
                this.setData({
                  duration: this.properties
                    .mf_data.duration || 300
                })
              }, 16.7)
            })
          }, this.data.duration)
        });
        i += 1
      }
      const s = {
        currentTarget: {
          dataset: {
            index: i
          }
        }
      };
      this.bindClick(s)
    }
  }
});
